package br.com.agiledev.webscrum.action;

import java.util.Arrays;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import br.com.agiledev.webscrum.eao.ProductEAO;
import br.com.agiledev.webscrum.eao.ReleaseEAO;
import br.com.agiledev.webscrum.entity.Product;
import br.com.agiledev.webscrum.entity.Release;
import br.com.agiledev.webscrum.entity.Sprint;

@SuppressWarnings("serial")
@Name("sprintSearchMB")
@Scope(ScopeType.PAGE)
public class SprintSearchAction extends QueryAction<Sprint> {

    private static final String EJBQL = "select sp from Sprint sp";
    private static final String[] RESTRICTIONS = {"lower(sp.nome) like lower(concat(#{sprintSearchMB.sprint.nome}, '%'))",
	"sp.release = #{sprintSearchMB.release}",
	"sp.release.product = #{sprintSearchMB.product}"};
    private static final String ORDER_COLUMN = "sp.release.product.nome, sp.release.nome, sp.nome";

    @In
    ProductEAO productEAO;
    
    @In
    ReleaseEAO releaseEAO;
    
    private Sprint sprint;

    private Release release;
    
    private Product product;
    
    private List<Product> products;
    
    private List<Release> releases;
    
    public SprintSearchAction() {
	setEjbql(EJBQL);
	setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	setOrder(ORDER_COLUMN);
    }
    
    //Get's / Set's
    public Sprint getSprint() {
	if(sprint == null) {
	    sprint = new Sprint();
	}
        return sprint;
    }

    public void setSprint(Sprint sprint) {
        this.sprint = sprint;
    }

    public List<Product> getProducts() {
	if(products == null) {
	    products = productEAO.findAll();
	}	
        return products;
    }

    public void setProducts(List<Product> products) {	
        this.products = products;
    }

    public List<Release> getReleases() {
	if(releases == null && getProduct() != null && getProduct().getOid() != null) {
	    releases = getProduct().getReleases();
	}	
        return releases;
    }

    public void setReleases(List<Release> releases) {
        this.releases = releases;
    }

    public Release getRelease() {
        return release;
    }

    public void setRelease(Release release) {
        this.release = release;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
}